home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / UTILITY2 / W_ONE41.ZIP / SPLIT.CPP < prev    next >
C/C++ Source or Header  |  1993-09-30  |  3KB  |  140 lines

  1. #include "split.h"
  2.  
  3. /******************************************************************\
  4. *                                                                  *
  5. *           w       w                oooo                           *
  6. *           w       w  iii  n   n   o    o   n   n  eeee            *
  7. *           w       w   i   nn  n  o      o  nn  n  e               *
  8. *           w   w   w   i   n n n  o      o  n n n  eee             *
  9. *              w w w w    i   n  nn   o    o   n  nn  e               *
  10. *              w   w    iii  n   n    oooo    n   n  eeee            *
  11. *                                                                      *
  12. *     C o m m a n d   L a n g u a g e   I n t e r p r e t e r      *
  13. *                                                                      *
  14. *                                                                      *
  15. *    Written by Lucien Cinc                                         *
  16. *    Copyright (c) 1992, 1993                                       *
  17. *                                                                  *
  18. \******************************************************************/
  19.  
  20. int split(char *path, long offset);
  21.  
  22. int main(void)
  23. {
  24.     int offset;
  25.     char *sp;
  26.  
  27.     sp = args();                // parse command line switches
  28.     while(*sp)
  29.         switch(*sp++) {
  30.             case 'v' :          // show version information
  31.                        printf("%cVersion %c%d.%01d\n", WHITE, YELLOW, VERSION / 10, VERSION % 10);
  32.                        return 0;
  33.             default:            // invalid switch
  34.                        perror("Invalid switch");
  35.                        return 1;
  36.         }
  37.  
  38.     if (argnstr()) {            // no string arguments
  39.         perror("Invalid argument");
  40.         return 1;
  41.     }
  42.  
  43.     if (argc() != 2) {
  44.         perror("Too many or few arguments");
  45.         return 1;
  46.     }
  47.  
  48.     return split(argabs(1), atol(argv(2)));    // split the file
  49. }
  50.  
  51. int copy(int src, char *path, long num)
  52. {
  53.  
  54.     char *buffer;
  55.     unsigned w;
  56.     int dst, size;
  57.     long count;
  58.  
  59.     if ((buffer = (char *)malloc(BUFSIZE)) == NULL) {    // get some space
  60.         perror("Out of memory");
  61.         return 0;
  62.     }
  63.  
  64.     if (_dos_creatnew(path, 0, &dst)) {                    // create the destination file
  65.         perror("Destination file already exists");
  66.         return 0;
  67.     }
  68.  
  69.     printf("%c%s %c%9ld%c\n", GREEN, unixpath(padfilename(path)), YELLOW, num, LIGHTGRAY);
  70.  
  71.     count = 0;
  72.     while (count < num) {
  73.  
  74.         size = BUFSIZE;                // next buffer size
  75.         if (count + size >= num)
  76.             size = num - count;
  77.  
  78.         if (_dos_read(src, buffer, size, &w) || w != size) {
  79.             perror("Read error");
  80.             break;
  81.         }
  82.  
  83.         if (_dos_write(dst, buffer, size, &w) || w != size) {
  84.             perror("Insuffient disk space");
  85.             break;
  86.         }
  87.  
  88.         if (isbreak())
  89.             break;
  90.  
  91.         inc(size);    // update status bar
  92.  
  93.         count += size;
  94.     }
  95.  
  96.     free(buffer);
  97.     _dos_close(dst);
  98.  
  99.     if (count == num)
  100.         return 1;    // ok
  101.  
  102.     return 0;
  103. }
  104.  
  105. int split(char *path, long offset)
  106. {
  107.     int handle;
  108.     long flen;
  109.     char temppath[MAXPATH];
  110.  
  111.     strcpy(temppath, path);
  112.  
  113.     if (_dos_open(path, O_RDONLY, &handle)) {
  114.         perror("Invalid path or file name");
  115.         return 1;
  116.     }
  117.  
  118.     flen = filelength(handle);
  119.     if (offset < 0 || offset > flen) {                // range check
  120.         perror("Bad split size");
  121.         _dos_close(handle);
  122.         return 1;
  123.     }
  124.  
  125.     limit(flen);    // status bar limit
  126.  
  127.     strcpy(strrchr(temppath, '.'), ".1");            // first half
  128.     if (copy(handle, temppath, offset)) {
  129.  
  130.         strcpy(strrchr(temppath, '.'), ".2");        // second half
  131.         copy(handle, temppath, flen - offset);
  132.  
  133.     }
  134.  
  135.     _dos_close(handle);
  136.     empty();        // finished with status bar
  137.  
  138.     return 0;
  139. }
  140.